import { request } from '../request';

// 字典项类型定义
export interface DictItem {
  id: number;
  dictType: string;
  itemCode: string;
  itemName: string;
  description?: string;
  createBy: string;
  createTime: string;
  updateBy: string;
  updateTime: string;
}

// 字典搜索参数
export interface DictSearchParams {
  current?: number;
  size?: number;
  dictType?: string;
  itemCode?: string;
  itemName?: string;
}

/**
 * 获取字典项列表
 *
 * @param params 搜索参数
 */
export function fetchDictItemList(params?: DictSearchParams) {
  return request<{
    records: DictItem[];
    total: number;
    current: number;
    size: number;
  }>({
    url: '/api/system/dict-items/page',
    method: 'GET',
    params
  });
}

/** 获取所有字典类型 */
export function fetchAllDictTypes() {
  return request<string[]>({
    url: '/api/system/dict-items/types',
    method: 'GET'
  });
}

/**
 * 删除字典项
 *
 * @param id 字典项ID
 */
export function deleteDictItem(id: number) {
  return request({
    url: `/api/system/dict-items/${id}`,
    method: 'DELETE'
  });
}

/**
 * 批量删除字典项
 *
 * @param ids 字典项ID数组
 */
export function batchDeleteDictItems(ids: number[]) {
  return request({
    url: '/api/system/dict-items/batch',
    method: 'DELETE',
    data: ids
  });
}

/**
 * 保存字典项（创建或更新）
 *
 * @param data 字典项数据
 */
export function saveDictItem(data: Partial<DictItem>) {
  const { id, ...rest } = data;
  const isUpdate = Boolean(id);

  return request<DictItem>({
    url: isUpdate ? `/api/system/dict-items/${id}` : '/api/system/dict-items',
    method: isUpdate ? 'PUT' : 'POST',
    data: isUpdate ? { ...rest, id } : rest
  });
}

/**
 * 获取字典项详情
 *
 * @param id 字典项ID
 */
export function getDictItemById(id: number) {
  return request<DictItem>({
    url: `/api/system/dict-items/${id}`,
    method: 'GET'
  });
}
